function convert_image()
% This program is using for convert every jpg file in the same folder into desctop resolution. Also, every jpg with CMYK code will be converted into RGB code. This function require ImageMagick program (google it). Converted images are placed in "Converted_picture" folder.
% updated at 2010/11/30, HY
% ==switch from PTB3 to PTB 2.54


picture_list = dir('*.jpg');
mkdir('Converted_picture');

%Setup Psychtoolbox
vinfo.bgcolor = 0;
vinfo.fgcolor = 255;
% SetResolution(0, 1024, 768);
% Screen('Preference', 'SkipSyncTests', 1);
[vinfo.p0 fullscreen_rect] = Screen(0, 'OpenWindow', vinfo.bgcolor);
HideCursor;
vinfo.sc_width = fullscreen_rect(3);
vinfo.sc_height = fullscreen_rect(4);


for i = 1:length(picture_list),
  fprintf('Converting %s\n', picture_list(i).name);
  try
    dos(sprintf('convert -colorspace rgb "%s" tmp.jpg', picture_list(i).name));
  catch
    copyfile(picture_list(i).name, 'tmp.jpg');
    disp('Warning, ImageMagick not found. CMYK file will corrupt');
  end;
  load_picture = imread('tmp.jpg');
  sz = size(load_picture);
  original_height = sz(1);
  original_width = sz(2);
  width_ratio = original_width / 1024;
  height_ratio = original_height / 768;
  
  if width_ratio > height_ratio,
    resized_width = floor(original_width / width_ratio);
    resized_height  = floor(original_height / width_ratio);
  else
    resized_width = floor(original_width / height_ratio);
    resized_height  = floor(original_height / height_ratio);
  end;

  picture_rect = [1024 / 2 - resized_width / 2, ...
    768 / 2 - resized_height / 2, ...
    1024 / 2 + resized_width / 2, ...
    768 / 2 + resized_height / 2];
  resized_width
  resized_height
  picture_rect
  Screen(vinfo.p0, 'FillRect', vinfo.bgcolor);
  Screen(vinfo.p0, 'PutImage', load_picture, picture_rect);
  imageArray=SCREEN(vinfo.p0,'GetImage');
  imwrite(imageArray, sprintf('Converted_picture\\%s', picture_list(i).name));
  delete('tmp.jpg');
end;

Screen('CloseAll'); 